﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using fanfu.Common;
using fanfu.Model;
using System.Data.SqlClient;

namespace fanfu.Business
{
    public class ArticleRule
    {
        //----------后台调用------------

        #region 获取所有文章信息
        /// <summary>
        /// 获取所有文章信息
        /// </summary>
        public DataTable GetAllArticleInfo()
        {
            string sqlString = "SELECT * FROM Article ORDER BY CreateTime DESC";
            DataTable dt = SqlHelper.GetDataTable(sqlString);
            return dt;
        }
        #endregion

        #region 获取所有正常状态的文章信息
        /// <summary>
        /// 获取所有正常状态的文章信息
        /// </summary>
        public DataTable GetAllNormalFlagArticleInfo()
        {
            string sqlString = "SELECT * FROM Article WHERE Flag=" + (int)ArticleFlag.normal + " ORDER BY CreateTime DESC";
            DataTable dt = SqlHelper.GetDataTable(sqlString);
            return dt;
        }
        #endregion

        #region 获取指定ID的文章
        /// <summary>
        /// 获取指定ID的文章
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ArticleModel GetArticleByID(string ID)
        {
            string sqlString = "SELECT TOP 1 * FROM Article WHERE ID=@ID";
            SqlParameter para = new SqlParameter("@ID", ID);
            DataTable dt = SqlHelper.GetDataTable(sqlString, para);
            if (dt.Rows.Count == 1)
                return new ArticleModel(dt.Rows[0]);
            else
                return null;
        }
        #endregion

        #region 删除指定ID的文章
        /// <summary>
        /// 删除指定ID的文章
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int DeleteArticle(string ID)
        {
            string sqlString = string.Format("UPDATE Article SET Flag={0} WHERE ID={1}", (int)ArticleFlag.delete, ID);
            return SqlHelper.ExecuteCommand(sqlString);
        }
        #endregion

        #region 文章标签枚举值转换
        /// <summary>
        /// 文章标签枚举值转换
        /// </summary>
        public string GetArticleTag(object objTag)
        {
            string strResult = string.Empty;
            int flag = Convert.ToInt32(objTag);
            switch (flag)
            {
                case (int)ArticleTag.html_css: strResult = "Html/Css"; break;
                case (int)ArticleTag.html5_css3: strResult = "Html5/Css3"; break;
                case (int)ArticleTag.javascript: strResult = "JavaScript"; break;
                case (int)ArticleTag.jQuery: strResult = "JQuery"; break;
                case (int)ArticleTag.net_sql: strResult = ".NET/SQL"; break;
                case (int)ArticleTag.actionScript: strResult = "ActionScript"; break;
                case (int)ArticleTag.qdtrifles: strResult = "前端琐事"; break;
                case (int)ArticleTag.personal: strResult = "个人随笔"; break;
                case (int)ArticleTag.other: strResult = "其它"; break;
            }
            return strResult;
        }
        #endregion

        //----------前台调用------------

        #region 获取文章标签信息、标签的总和并返回List<string[]>
        /// <summary>
        /// 获取文章标签信息、标签的总和
        /// </summary>
        /// <returns></returns>
        public List<string[]> GetArticleTagList()
        {
            List<string[]> lstArticleTag = new List<string[]>();
            foreach (object enumValue in Enum.GetValues(typeof(ArticleTag)))
            {
                Enum e = (Enum)enumValue;
                string desc = EnumPlus.GetEnumDesc(e);
                string value = ((int)enumValue).ToString();
                string sqlString = "SELECT COUNT(ID) FROM Article WHERE Flag="+(int)ArticleFlag.normal+" AND ArticleTag=" + value;
                string count = SqlHelper.GetScalar(sqlString).ToString();
                string[] temp = { value, desc, count };
                lstArticleTag.Add(temp);
            }
            return lstArticleTag;
        }
        #endregion

        #region 获取文章归档信息列表并返回List<string[]>
        /// <summary>
        /// 获取文章归档信息列表
        /// </summary>
        /// <returns></returns>
        public List<string[]> GetArchivesList()
        {
            List<string[]> lstArchives = new List<string[]>();
            string sqlGroup = "SELECT Year(CreateTime)AS Year,Month(CreateTime)AS Month,Count(ID)AS Num FROM Article WHERE Flag="
                + (int)ArticleFlag.normal+" GROUP BY Year(CreateTime),Month(CreateTime) ORDER BY Year(CreateTime) DESC,Month(CreateTime) DESC";
            DataTable  dtArchives = SqlHelper.GetDataTable(sqlGroup);
            foreach (DataRow dr in dtArchives.Rows)
            {
                string zhDateTime = dr["Year"].ToString() + "年 " + dr["Month"].ToString() + "月";
                string enDateTime = dr["Year"].ToString() + "-" + dr["Month"].ToString();
                string[] temp = { enDateTime, zhDateTime, dr["Num"].ToString() };
                lstArchives.Add(temp);
            }
            return lstArchives;
        }
        #endregion

        #region 创建文章标签HTML
        /// <summary>
        /// 创建文章标签HTML
        /// </summary>
        /// <returns></returns>
        public StringBuilder CreateArticleTagHtml()
        {
            StringBuilder articleTag = new StringBuilder();
            List<string[]> lstArticleTag = new ArticleRule().GetArticleTagList();
            foreach (string[] tag in lstArticleTag)
            {
                if (tag[1] != "全部")
                    articleTag.AppendFormat("<li><a href=\"Index.aspx?current=0&type=articleTag&articleTag={0}&page=1\">{1}</a> ({2})</li>", tag[0], tag[1], tag[2]);
            }
            return articleTag;
        }
        #endregion

        #region 创建文章归档HTML
        /// <summary>
        /// 创建全部条数,文章归档HTML
        /// </summary>
        /// <returns></returns>
        public StringBuilder CreateArchivesHtml()
        {   
            StringBuilder archives = new StringBuilder();
            List<string[]> lstArchives = new ArticleRule().GetArchivesList();
            foreach (string[] arch in lstArchives)
            {
                archives.AppendFormat("<li><a href=\"Index.aspx?current=0&type=archives&archives={0}&page=1\">{1}</a> ({2})</li>", arch[0], arch[1], arch[2]);
            }
            return archives;
        }

        /// <summary>
        /// 创建指定条数,文章归档HTML
        /// </summary>
        /// <param name="topNum">创建的条数</param>
        /// <returns></returns>
        public StringBuilder CreateArchivesHtml(int topNum)
        {
            StringBuilder archives = new StringBuilder();
            List<string[]> lstArchives = new ArticleRule().GetArchivesList();
            int tempNum = 0;
            if (topNum >= lstArchives.Count)
            {
                foreach (string[] arch in lstArchives)
                {
                    archives.AppendFormat("<li><a href=\"Index.aspx?current=0&type=archives&archives={0}&page=1\">{1}</a> ({2})</li>", arch[0], arch[1], arch[2]);
                }
            }
            else
            {
                foreach (string[] arch in lstArchives)
                {
                    if (tempNum == topNum)
                        break;
                    archives.AppendFormat("<li><a href=\"Index.aspx?current=0&type=archives&archives={0}&page=1\">{1}</a> ({2})</li>", arch[0], arch[1], arch[2]);
                    tempNum++;
                }
            }
            return archives;
        }
        #endregion

        #region 创建热门文章HTML
        /// <summary>
        /// 创建热门文章HTML
        /// </summary>
        /// <param name="topNum">需要读取的条数</param>
        /// <param name="cutNum">截取字符串的长度</param>
        /// <returns></returns>
        public StringBuilder CreateHotArticleHtml(int topNum, int cutNum)
        {
            StringBuilder hotArticle = new StringBuilder();
            string sqlString = "SELECT Top " + topNum + " ID,Caption FROM Article WHERE Flag=" 
                + (int)ArticleFlag.normal + " ORDER BY SetTop ASC, BrowserCount DESC, CreateTime DESC";
            DataTable dt = SqlHelper.GetDataTable(sqlString);
            foreach (DataRow dr in dt.Rows)
            {
                hotArticle.AppendFormat("<li><a title=\"{0}\" href=\"Article.aspx?current=0&id={1}\">{2}</a></li>", dr["Caption"], dr["ID"], StringPlus.StringDot(dr["Caption"].ToString(), cutNum));
            }
            return hotArticle;
        }
        #endregion

        #region 文章类型枚举值创建
        /// <summary>
        /// 文章类型枚举值创建
        /// </summary>
        /// <param name="objType"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetArticleType(object objType, string url)
        {
            string strResult = string.Empty;
            int type = Convert.ToInt32(objType);
            switch (type)
            {
                case (int)ArticleType.original: strResult = "&nbsp;|&nbsp;作者原创"; break;
                case (int)ArticleType.reprint: strResult = "&nbsp;|&nbsp;转载自:<a target='_blank' title='" + url + "' href='" + url + "'>" + StringPlus.StringDot(url, 25) + "</a>"; break;
            }
            return strResult;
        }
        #endregion

        #region 文章浏览量总数加一
        /// <summary>
        /// 文章浏览量总数加一
        /// </summary>
        /// <param name="id">需要加一的文章ID</param>
        public void BrowserCountPlusOne(string id)
        {
            string sqlString = "UPDATE Article SET BrowserCount=BrowserCount+1 WHERE ID=" + id;
            SqlHelper.ExecuteCommand(sqlString);
        }

        #endregion

        #region 文章评论总数加一
        /// <summary>
        /// 文章评论总数加一
        /// </summary>
        /// <param name="id">需要加一的文章ID</param>
        public void CommentCountPlusOne(string id)
        {
            string sqlString = "UPDATE Article SET CommentCount=CommentCount+1 WHERE ID=" + id;
            SqlHelper.ExecuteCommand(sqlString);
        }
        #endregion

        #region 文章评论总数更新
        /// <summary>
        /// 文章评论总数更新
        /// </summary>
        /// <param name="id">需要更新的文章ID</param>
        /// <param name="commentCount">需要更新的值</param>
        public void ArticleCommentCountUpdate(string id, int commentCount)
        {
            string sqlString = "UPDATE Article SET CommentCount=" + commentCount + " WHERE ID=" + id;
            SqlHelper.ExecuteCommand(sqlString);
        }
        #endregion
    }
}
